package org.dromara.chat.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.chat.domain.vo.ChatMessageVo;
import org.dromara.chat.domain.vo.ChatSessionVo;
import org.dromara.chat.service.AiKnowledgeChatService;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库聊天控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat/knowledge")
public class AIKnowledgeChatController extends BaseController {

    private final AiKnowledgeChatService knowledgeChatService;

    /**
     * 基于知识库进行智能问答
     */
    @SaCheckPermission("chat:knowledge:chat")
    @Log(title = "知识库问答", businessType = BusinessType.OTHER)
    @PostMapping("/chat")
    public R<ChatMessageVo> chatWithKnowledge(
            @RequestParam Long sessionId,
            @RequestParam Long userId,
            @RequestParam String question,
            @RequestParam Long knowledgeId,
            @RequestParam String modelName) {
        ChatMessageVo response = knowledgeChatService.chatWithKnowledge(sessionId, userId, question, knowledgeId, modelName);
        return R.ok(response);
    }

    /**
     * 创建知识库聊天会话
     */
    @SaCheckPermission("chat:knowledge:session")
    @Log(title = "创建知识库会话", businessType = BusinessType.INSERT)
    @PostMapping("/session")
    public R<ChatSessionVo> createKnowledgeSession(
            @RequestParam Long userId,
            @RequestParam Long knowledgeId,
            @RequestParam(required = false) String sessionTitle) {
        ChatSessionVo session = knowledgeChatService.createKnowledgeSession(userId, knowledgeId, sessionTitle);
        return R.ok(session);
    }

    /**
     * 搜索知识库内容
     */
    @SaCheckPermission("chat:knowledge:search")
    @GetMapping("/search")
    public R<List<String>> searchKnowledgeContent(
            @RequestParam Long knowledgeId,
            @RequestParam String query,
            @RequestParam(defaultValue = "5") Integer limit) {
        List<String> results = knowledgeChatService.searchKnowledgeContent(knowledgeId, query, limit);
        return R.ok(results);
    }

    /**
     * 构建知识库上下文
     */
    @SaCheckPermission("chat:knowledge:context")
    @GetMapping("/context")
    public R<String> buildKnowledgeContext(
            @RequestParam Long knowledgeId,
            @RequestParam String question) {
        String context = knowledgeChatService.buildKnowledgeContext(knowledgeId, question);
        return R.ok(context);
    }

    /**
     * 获取知识库聊天历史
     */
    @SaCheckPermission("chat:knowledge:history")
    @GetMapping("/history/{sessionId}")
    public R<List<ChatMessageVo>> getKnowledgeChatHistory(
            @PathVariable Long sessionId,
            @RequestParam(defaultValue = "20") Integer limit) {
        List<ChatMessageVo> history = knowledgeChatService.getKnowledgeChatHistory(sessionId, limit);
        return R.ok(history);
    }

}